﻿using System;
using cludg.Infrastructure.Core.Attributes;
using cludg.Infrastructure.Core.Settings;
using nsadv.Statistic.Domain;
using nsadv.Statistic.Repository;
using nsadv.Statistic.Services.Security;
using nsadv.Statistic.Web.Notifications.Application;

namespace nsadv.Statistic.Web.Bootstrappers
{
	public class TestDataBootstrapper : Bootstrapper
	{
		[Dependency]
		public IApplicationNotifier ApplicationNotifier { get; set; }
		[Dependency]
		public Administration Administration { get; set; }
		
		[Dependency]
		public IRepository<Banner> Banners { get; set; }
		[Dependency]
		public IRepository<Advertiser> Advertisers { get; set; }
		[Dependency]
		public IRepository<Platform> Platforms { get; set; }
		[Dependency]
		public IRepository<Place> Places { get; set; }
		[Dependency]
		public IRepository<Campaign> Campaigns { get; set; }
		[Dependency]
		public IRepository<StatisticItem> StatisticItems { get; set; }


		[Dependency]
		public ITransactionFactory TransactionFactory { get; set; }

		[Dependency]
		public ISettings Settings { get; set; }

		public override int Priority
		{
			get
			{
				return 100;
			}
		}

		public override void Execute()
		{
			if ((bool)Settings["Debug_CreateTestData"])
			{
				ApplicationNotifier.SessionStart += ApplicationNotifier_SessionStart;
			}
		}

		//void ApplicationNotifier_SessionStart(object sender, SessionEventArgs e)
		//{
		//    using (var tran = TransactionFactory.BeginTransaction())
		//    {
		//        // Добавляем платформы
		//        Platforms.Add(new Platform { Id = "iphone", Name = "Apple iPhone" });
		//        Platforms.Add(new Platform { Id = "android", Name = "Google Android" });
		//        Platforms.Add(new Platform { Id = "winmobile", Name = "Microsoft Windows Mobile" });

		//        // Рекламные площадки
		//        Places.Add(new Place { Id = "commersant", Name = "Коммерсантъ" });
		//        Places.Add(new Place { Id = "wmj", Name = "Woman Journal" });
		//        Places.Add(new Place { Id = "axelspringer", Name = "Axel Springer" });
		//        Places.Add(new Place { Id = "aif", Name = "АиФ" });
		//        Places.Add(new Place { Id = "comsomol", Name = "Комсомолка" });

		//        // Рекламодатели
		//        Advertisers.Add(new Advertiser { Id = "bmw", Name = "BMW" });
		//        Advertisers.Add(new Advertiser { Id = "cocacola", Name = "Coca-Cola" });
		//        Advertisers.Add(new Advertiser { Id = "imho", Name = "IMHO" });
		//        Advertisers.Add(new Advertiser { Id = "mobiledirect", Name = "MobileDirect" });
		//        Advertisers.Add(new Advertiser { Id = "wapstart", Name = "WapStart" });

		//        tran.Commit();
		//    }

		//    Campaign bmwCamp = null;
		//    Campaign colaClosedCamp = null;

		//    using (var tran = TransactionFactory.BeginTransaction())
		//    {
		//        // Кампании
		//        bmwCamp = new Campaign { Name = "рекламируем BMW!", Advertiser = Advertisers.ById("bmw"), StartTime = new DateTime(2008, 12, 3) };
		//        Campaigns.Add(bmwCamp);
		//        Campaigns.Add(new Campaign { Name = "рекламируем BMW еще раз!", Advertiser = Advertisers.ById("bmw"), StartTime = new DateTime(2009, 12, 3) });
		//        Campaigns.Add(new Campaign { Name = "рекламируем CocaCola!", Advertiser = Advertisers.ById("cocacola"), StartTime = new DateTime(2009, 12, 3) });
		//        colaClosedCamp = new Campaign { Name = "рекламируем CocaCola закрыт!", Advertiser = Advertisers.ById("cocacola"), StartTime = new DateTime(2009, 12, 3), EndTime = new DateTime(2010, 12, 1) };
		//        Campaigns.Add(colaClosedCamp);

		//        tran.Commit();
		//    }

		//    Banner b1, b2, b3, bc1, bc2;
		//    using (var tran = TransactionFactory.BeginTransaction())
		//    {
		//        // Банеры
		//        b1 = new Banner { Campaign = bmwCamp, Name = "Банер BMW 1" };
		//        Banners.Add(b1);
		//        b2 = new Banner { Campaign = bmwCamp, Name = "Банер BMW 2" };
		//        Banners.Add(b2);
		//        b3 = new Banner { Campaign = bmwCamp, Name = "Банер BMW 3" };
		//        Banners.Add(b3);

		//        bc1 = new Banner { Campaign = colaClosedCamp, Name = "Банер CocaCola 1" };
		//        Banners.Add(bc1);
		//        bc2 = new Banner { Campaign = colaClosedCamp, Name = "Банер CocaCola 2" };
		//        Banners.Add(bc2);

		//        tran.Commit();
		//    }


		//    using (var tran = TransactionFactory.BeginTransaction())
		//    {
		//        // Статистика
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now, Count = 500 });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("wmj"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("wmj"), EventType = EventType.Show, EventDate = DateTime.Now });

		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("commersant"), EventType = EventType.Click, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Click, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("commersant"), EventType = EventType.Click, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b1, Platform = Platforms.ById("winmobile"), Place = Places.ById("wmj"), EventType = EventType.Click, EventDate = DateTime.Now });

		//        StatisticItems.Add(new StatisticItem { Banner = b2, Platform = Platforms.ById("android"), Place = Places.ById("wmj"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b2, Platform = Platforms.ById("android"), Place = Places.ById("aif"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b2, Platform = Platforms.ById("winmobile"), Place = Places.ById("aif"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b2, Platform = Platforms.ById("android"), Place = Places.ById("wmj"), EventType = EventType.Show, EventDate = DateTime.Now });

		//        StatisticItems.Add(new StatisticItem { Banner = b2, Platform = Platforms.ById("winmobile"), Place = Places.ById("aif"), EventType = EventType.Click, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = b2, Platform = Platforms.ById("android"), Place = Places.ById("wmj"), EventType = EventType.Click, EventDate = DateTime.Now });


		//        StatisticItems.Add(new StatisticItem { Banner = bc1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = bc1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Show, EventDate = DateTime.Now });
		//        StatisticItems.Add(new StatisticItem { Banner = bc1, Platform = Platforms.ById("iphone"), Place = Places.ById("commersant"), EventType = EventType.Click, EventDate = DateTime.Now });

		//        tran.Commit();
		//    }

		//    var userRoot = Administration.AddUser("root", "1");
		//    Administration.MakeUserRoot(userRoot);

		//    ApplicationNotifier.SessionStart -= ApplicationNotifier_SessionStart;
		//}

		void ApplicationNotifier_SessionStart(object sender, SessionEventArgs e)
		{
			using (var tran = TransactionFactory.BeginTransaction())
			{
				// Добавляем платформы
				Platforms.Add(new Platform { Id = "iPhone OS", Name = "Apple iPhone" });

				// Рекламные площадки
				Places.Add(new Place { Id = "399", Name = "WMJ" });
				Places.Add(new Place { Id = "299", Name = "Axel Springer" });

				// Рекламодатели
				Advertisers.Add(new Advertiser { Id = "ttg1Adv", Name = "TTG" });
				Advertisers.Add(new Advertiser { Id = "3903Adv", Name = "NS Adv" });

				tran.Commit();
			}

			Campaign ttg1Camp = null;
			Campaign Camp3903 = null;

			using (var tran = TransactionFactory.BeginTransaction())
			{
				// Кампании
				ttg1Camp = new Campaign 
					{ 
						Id = "0", 
						Name = "TTG", 
						Advertiser = Advertisers.ById("ttg1Adv"), 
						StartTime = new DateTime(2009, 12, 1), 
						EndTime = new DateTime(2010, 12, 31), 
						IsActive = false 
					};
				Campaigns.Add(ttg1Camp);

				Camp3903 = new Campaign 
					{ 
						Id = "1", 
						Name = "Измерительный баннер", 
						Advertiser = Advertisers.ById("3903Adv"), 
						StartTime = new DateTime(2009, 12, 1), 
						EndTime = new DateTime(2010, 12, 31), 
						IsActive = true 
					};
				Campaigns.Add(Camp3903);

				tran.Commit();
			}

			using (var tran = TransactionFactory.BeginTransaction())
			{
				// Банеры
				Banners.Add(new Banner { Campaign = ttg1Camp, Name = "TTG 1", Id = "ttg1" });
				Banners.Add(new Banner { Campaign = Camp3903, Name = "Измерительный баннер", Id = "3903", IsTest = true });

				tran.Commit();
			}


			var userRoot = Administration.AddUser("root", "1");
			Administration.MakeUserRoot(userRoot);

			ApplicationNotifier.SessionStart -= ApplicationNotifier_SessionStart;
		}
	}
}